package service

import (
	"TodoList/dao"
	"TodoList/logm"
	"TodoList/models"
	"TodoList/serializer"
)

// DeleteTodoService 删除视频的服务
type DeleteTodoService struct {
	ID int64 `json:"id" form:"id"`
}

// Delete 删除视频函数
func (service *DeleteTodoService) Delete() serializer.Response {
	var todo models.Todo
	//数据库中查询到需要的视频
	res := dao.PgDB.Where("id=?", service.ID).First(&todo)
	if res.Error != nil {
		logm.Sad.Println("task not found:", res.Error.Error())
		return serializer.Response{
			Code:    501,
			Message: "事项不存在",
			Error:   res.Error.Error(),
		}
	}
	// 删除查找到的视频
	res = dao.PgDB.Delete(&todo)
	if res.Error != nil {
		logm.Sad.Println("delete task failed:", res.Error.Error())
		return serializer.Response{
			Code:    501,
			Message: "事项删除失败",
			Error:   res.Error.Error(),
		}
	}
	logm.Smile.Println("the task is deleted")
	return serializer.Response{
		Code:    0,
		Message: "事项删除成功",
	}
}
